<template>
  <div class='ele-body'>
    <!-- 搜索表单 -->
    <div class='block-interval'>
      <a-card :bordered='false'>
        <a-form layout='inline' :model='where'>
          <a-row>
            <a-form-item label='开始时间:'>
              <a-date-picker v-model:value='where.searchBeginTime' format='YYYY-MM-DD' valueFormat='YYYY-MM-DD' />
            </a-form-item>
            <a-form-item label='结束时间:'>
              <a-date-picker v-model:value='where.searchEndTime' format='YYYY-MM-DD' valueFormat='YYYY-MM-DD' />
            </a-form-item>
            <a-form-item class='ele-text-center'>
              <a-space>
                <a-button type='primary' @click='reload'>查询</a-button>
                <a-button @click='reset'>重置</a-button>
              </a-space>
            </a-form-item>

          </a-row>
        </a-form>
      </a-card>
    </div>

    <!-- 表格 -->
    <div>
      <a-card :bordered='false'>
        <ele-pro-table
          ref='table'
          row-key='dcdqId'
          :datasource='datasource'
          :columns='columns'
          :where='where'
          v-model:selection='selection'
          :scroll="{ x: 'max-content' }"
        >

          <template #bodyCell="{ column, record }">
            <!-- table操作栏按钮 -->
            <template v-if="column.key === 'action'">
              <a-space>
                <a @click="openDetail(record)">详情</a>
                <a-divider type="vertical" />
                <div >
                  <a-popconfirm title='确定要删除此记录吗？' @confirm='remove(record)'>
                    <a class='ele-text-danger'>删除</a>
                  </a-popconfirm>
                </div>
              </a-space>
            </template>
          </template>
        </ele-pro-table>
      </a-card>
    </div>

    <!-- 编辑弹窗 -->
    <DevicePowerMonitorConfigQueueDetail v-if='showDetail' v-model:visible="showDetail" :data="current"  />
  </div>
</template>

<script>
import {DeviceCommandDownQueueApi} from '@/api/property/DeviceCommandDownQueueApi';
import DevicePowerMonitorConfigQueueDetail from './powermonitor-config-queue-detail.vue'
import moment from 'moment'
import { message } from 'ant-design-vue';

export default {
  name: 'DevicePowerMonitorConfigQueue',
  components: { DevicePowerMonitorConfigQueueDetail },
  props: {
    data: Object
  },
  data() {
    return {
      // 表格列配置
      columns: [
        {
          title: '推送类型',
          dataIndex: 'sceneDictate',
          customRender: function(row) {
            if (row.text == 0) {
              return "推送时间到终端";
            } else if (row.text == 4) {
              return "重启终端设备";
            } else if (row.text == 5) {
              return "通知设备更新代码";
            } else if (row.text == 6) {
              return "更新终端网关的握手间隔时间";
            } else if (row.text == 7) {
              return "设置终端连接服务器的IP和端口";
            } else if (row.text == 25) {
              return "设置参数(全局配置和各种参数配置)"
            } else if (row.text == 32) {
              return "读取设备时间";
            } else if (row.text == 37) {
              return "读取终端网关的握手间隔时间";
            } else if (row.text == 56) {
              return "读取参数配置";
            } else if (row.text == 34) {
              return "读取设备id和固件版本";
            } else if (row.text == 38) {
              return "读取设备的SIM卡信息";
            }
          }
        },
        {
          title: '推送情况',
          dataIndex: 'isSend',
          customRender: function(row) {
            console.log(row)
            const isSend = row.text;
            if (isSend == 0) {
              return '未推送';
            } else if (isSend == 1) {
              return '推送中';
            } else if (isSend == 2) {
              return '已推送等反馈';
            } else if (isSend == 3) {
              if (row.record.feedback == 1) {
                return '已反馈(确认)';
              } else if (row.record.feedback == 2){
                return '已反馈(否认)';
              } else {
                return '已反馈(未知)';
              }
            } else if (isSend == 4) {
              return '失败';
            }
          }
        },
        {
          title: '执行信息',
          dataIndex: 'msg',
          customRender : function (row) {
            if (row.text) {
              return row.text;
            } else {
              return '-'
            }
          }
        },
        {
          title: '推送时间',
          dataIndex: 'sendTime',
          customRender : function (row) {
            if (row.text) {
              return moment(row.text * 1000).format('YYYY-MM-DD HH:mm:ss')
            } else {
              return '-';
            }
          }
        },
        {
          title: '创建时间',
          dataIndex: 'createTime'
        },
        {
          title: '操作者',
          dataIndex: 'createUserName'
        },
        {
          title: '操作',
          key: 'action',
          align: 'center'
        }
      ],
      // 表格搜索条件
      where: { 'recordId': this.data.recordId },
      // 表格选中数据
      selection: [],
      // 当前编辑数据
      current: null,
      // 是否显示编辑弹窗
      showDetail: false
    };
  },
  methods: {
    /**
     * 搜索按钮
     *
     * @author cancan
     * @date 2022/03/07 00:58
     */
    reload() {
      this.selection = [];
      this.$refs.table.reload({ page: 1 });
    },

    /**
     * 重置搜索
     *
     * @author cancan
     * @date 2022/03/07 00:58
     */
    reset() {
      this.where.recordId = this.data.recordId;
      this.where.searchBeginTime = '';
      this.where.searchEndTime = '';
      this.$nextTick(() => {
        this.reload();
      });
    },

    /**
     * 删除
     *
     * @author cancan
     * @date 2022/03/07 00:58
     */
    async remove(row) {
      const result = await DeviceCommandDownQueueApi.delete({ dcdqId: row.dcdqId });
      message.success(result.message);
      this.reload();
    },

    openDetail(row) {
      this.current = row;
      this.showDetail = true;
    },

    /**
     * 获取表格数据
     *
     * @author fengshuonan
     * @date 2022/5/8 15:18
     */
    datasource({ page, limit, where, orders }) {
      return DeviceCommandDownQueueApi.findPage({ ...where, ...orders, pageNo: page, pageSize: limit });
    }
  }
};
</script>
